package smodel;

import javax.swing.text.PlainDocument;

import tcp.TCPClient;
import tcp.TCPObserver;


/**
 * This is the slave/client end of a shared model.
 * It simply obeys whatever is sent over the wire.
 * 
 * 
 * @author Kristopher L. Kalish, James Madison University
 * @version 1 - April 11, 2008
 *
 * @param <T>
 */
public class NodeModel<T extends NamedModel> extends AbstractModel<T> implements TCPObserver
{
	private TCPClient tcpClient;

	public NodeModel(String host, int port)
	{
		tcpClient = new TCPClient(host, port);
		tcpClient.addObserver(this);
	}
	
	/**
	 * We have a contract that the master will talk and we'll solely
	 * listen.  Objects coming over this port should be considered 
	 * models.
	 */
	public void handleObject(Object o)
	{	
		System.out.println("Got a new state!: " + o); //debug
		T newState = (T) o;
		
		// only change state if the name matches
		if(newState.getName().equals(getState().getName()))
			setState(newState);
	}
}
